/**
 * @ignore
 */
Ext.data.ProxyMgr.registerType("categorystorage",
    Ext.extend(Ext.data.LocalStorageProxy, {
    	id  : 'netgis-category-storage'
		/*
        ,create: function(operation, callback, scope) {
        	Ext.data.LocalStorageProxy.create.call(this, operation, callback, scope);
        }

        ,read: function(operation, callback, scope) {
            var thisProxy = this;
        }

        ,update: function(operation, callback, scope) {
        }

        ,destroy: function(operation, callback, scope) {
        }
        */
    })
);

/**
 * @class NG.models.Category
 * @extends Ext.data.Model 
 * 
 * Model represents the categories of map.<br>
 * Model of category is registered as 'NG.models.Category'.<br>
 * Store of category is registered as 'categorystorage'.
 */
NG.models.Category = Ext.regModel("NG.models.Category", {
	
	/**
	 * Fields of category model:
	 * <li>id
	 * <li>refKeyword
	 * <li>refName
	 * <li>catName
	 * <li>catActive
	 * <li>catVisible
	 * <li>catID
	 * <li>iconType
	 * <li>startScale
	 * <li>endScale
	 * <li>subCategories: Array
	 * <li>visibility: Boolean default value is <b>true</b>. Used for interface operations.
	 * <li>selected: Boolean default value is <b>false</b>. Used for putinfoex input type operation. Input will be made according to selected category. 
	 * 
	 * @property fields
	 * @type Array
	 */
    fields: [
        {name: "id", type: "int"},
        {name: "refKeyword", type: "string"},
        {name: "refName", type: "string"},
        {name: "catName", type: "string"},
        {name: "catActive", type: "int"},
        {name: "catVisible", type: "int"},
        {name: "catID", type: "string"},
        {name: "iconType", type: "string"},
        {name: "startScale", type: "double"},
        {name: "endScale", type: "double"},
        {name: "subCategories"},
        {name: "visibility", defaultValue: true},
        {name: "selected", defaultValue: false}
   	],
   	
   	/**
   	 * @private
   	 */
   	phantom: true,
   	
   	/**
   	 * 
   	 * @property proxy
   	 * <li>type: String default values is 'categorystorage'.
   	 * @type Object
   	 */
   	proxy: {
   		type:'categorystorage'
   	},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getId: function (){return this.data.id;},
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getRefKeyword: function (){return this.data.refKeyword;},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getRefName: function (){return this.data.refName;},
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getCatName: function (){return this.data.catName;},
   	/**
   	 * 
   	 * @return {Integer}
   	 */
   	getCatActive: function (){return this.data.catActive;},
   	getCatVisible: function (){return this.data.catVisible;},
   	getCatID: function (){return this.data.catID;},
   	getIconType: function (){return this.data.iconType;},
   	getStartScale: function(){return this.data.startScale;},
   	getEndScale: function(){return this.data.endScale;},
   	getSubCategories: function() {return this.data.subCategories;},
   	
   	/**
   	 * 
   	 * @return {Boolean} category is selected or not. 
   	 */
   	isSelected: function() {return this.data.selected;},
   	
   	/**
   	 * 
   	 * @param {Boolean} selected
   	 */
   	setSelected: function(selected) {this.data.selected = selected;},
   	
   	/**
   	 * This method is called when a model is being listed in a List.
   	 * 
   	 * @return {String} Text when category is being listed in a List view 
   	 */
   	getTableDisplayValue: function (){
   		return this.getCatName();
   	},
   	
   	/**
   	 * Category refName must be in such a format that "xxx.yyy.zzz". Otherwise returns null.<br>
   	 * <b>options</b> param consists of<br>
   	 * <li>successFn Function.
   	 * <li>failureFn Function.
   	 * 
   	 * @param {Object} options
   	 */
   	findFAD: function (options){
   		var refName = this.getRefName();
   		var splitArray = refName.split(".");
   		if (splitArray.length !== 3) {
   			alert("Kategori ref name xxx.yyy.zzz formatında olmalı");
   			return;
   		}
   			
   		NGLogger.info("Category Ref Name: " + refName);
   		var datasetName = splitArray[0];
   		var tableName = splitArray[1];
   		var fadTableName = tableName + "." + datasetName + "." + tableName;
   		NGLogger.info("FAD Table Name: " + fadTableName);
   		
   		//This is called when fad list loaded, used in loadFAD dispatch below.
   		var successFn = function (fadList, sessionID){
   			var selectedFAD = null;
   			for ( var i = 0; i < fadList.length; i++) {
				var fad = fadList[i];
				if (fadTableName === fad.getTableName()) {
					selectedFAD = fad;
					break;
				}
			}
   			if (Ext.isFunction (options.successFn))
   				options.successFn.call(this, selectedFAD, sessionID);
   		};
   		
   		//loads fad, successFn is called when fad are loaded.
   		Ext.dispatch({
   			controller: app.controllers.mapView,
   			action: 'loadFAD',
   			workspace: app.views.mapView.getWorkspace(),
   			successFn: successFn,
   			failureFn: function (response){
   				alert("Error " + response);
   			}
   		});
   	},
   	
   	/**
   	 * This check is done related to iconType property.
   	 * @return {Boolean} category is polygon or not.
   	 */
   	isGeoPolygon: function (){
   		return this.data.iconType.indexOf("AREA") >= 0;
   	},

   	/**
   	 * This check is done related to iconType property.
   	 * @return {Boolean} category is line(path) or not.
   	 */
   	isGeoPath: function (){
   		return this.data.iconType.indexOf("LINE") >= 0;   		   		
   	},

   	/**
   	 * This check is done related to iconType property.
   	 * @return {Boolean} category is point or not.
   	 */
   	isGeoPoint: function (){
   		return this.data.iconType.indexOf("POINT") >= 0;   		
   	},
   	
	/**
	 * 
	 * @param {String} category
	 * @return {Boolean}
	 */
   	equalsTo: function (category) {
   		if (Ext.isEmpty(category) || Ext.isEmpty(category.getCatName())) {
   			return false;
   		}
   		return this.getCatName() === category.getCatName();
   	}
		
});

/**
 * @class NG.stores.Category
 * @extend Ext.data.Store
 */
NG.stores.Category = Ext.extend(Ext.data.Store, {
	/**
	 * Default values is 'NG.models.Category'. 
	 * @property model
	 * @type String
	 */
    model: "NG.models.Category",
    
    /**
     * Default value is 'catName'.
     * @property sorters
     * @type String
     */
    sorters: 'catName',
    
    /**
     * If category list will be grouped, the criteria of grouping will be this function result.
     * @param {Ext.data.Model} record
     * @return {String}
     */
    getGroupString : function(record) {
        return record.get('name')[0];
    }
});
